package com.jmdz.fmis.admin.api.message;


import com.jmdz.fmis.core.exception.AdminServiceException;
import com.jmdz.fmis.core.exception.ExceptionDefinition;
import com.jmdz.fmis.core.exception.ServiceException;
import com.jmdz.fmis.data.model.Page;
import com.jmdz.fmis.data.domain.MessageInfoDO;
import com.jmdz.fmis.data.mapper.MessageInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.RowBounds;
import java.util.Date;
import java.util.List;

/**
 * Generate Code By Fmis
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageInfoMapper messageInfoMapper;

    @Override
    public boolean delete(Long id,  Long adminId) throws ServiceException {
        return messageInfoMapper.deleteById(id) > 0;
    }

    @Override
    public Page<MessageInfoDO> list(Integer messageType, Integer page, Integer limit, Long adminId) throws ServiceException {
    QueryWrapper<MessageInfoDO> wrapper = new QueryWrapper<MessageInfoDO>();
        if (messageType != null) {
            wrapper.like("message_type", messageType);
        }
        return messageInfoMapper.selectPage(Page.div(page, limit, MessageInfoDO.class), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageInfoDO create(Integer messageType, Long adminId) throws ServiceException {
         Date now = new Date();
         MessageInfoDO insertDO = new MessageInfoDO();
         insertDO.setMessageType(messageType);
         insertDO.setGmtUpdate(now);
         insertDO.setGmtCreate(now);
         if (messageInfoMapper.insert(insertDO) > 0) {
             return insertDO;
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, Integer messageType, Long adminId) throws ServiceException {
         Date now = new Date();
         MessageInfoDO updateDO = new MessageInfoDO();
         updateDO.setId(id);
         updateDO.setMessageType(messageType);
         updateDO.setGmtUpdate(now);
         if (messageInfoMapper.updateById(updateDO) > 0) {
             return "ok";
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

}
